package com.greensoftcodeplus.redislog.component;

import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.core.PriorityOrdered;

public class MyBeanPostProcessor implements BeanPostProcessor, PriorityOrdered {
    ApplicationContext applicationContext;
      public    MyBeanPostProcessor(ApplicationContext applicationContext   )
      {
        this.applicationContext=applicationContext;
      }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        if (beanName.equals("redisConnectionFactory")) {    //"redisConnectionFactory" bean名限制了只对redis的RedisConnection增强

            // 使用日志代理，覆盖原Redis工厂类
            //if (!StringUtils.isEmpty(env.getProperty("logging.level." + RedisFactoryBean.class.getName()))) {
              /*注意：getProxy函数第二个参数nvocation ->{
                          return  new RedisAdvice().interceptorRedisFactory(invocation)
                  1. MethodInterceptor 作为接口的实现类，其中return  new RedisAdvice().interceptorRedisFactory(invocation)式接口 Object invoke(MethodInvocation invocation) throws Throwable
                  的实现方法
                  2.函数表达式作为增强使用 ，增强后调用表达式
                  3.invocation 是接口 函数Object invoke(MethodInvocation invocation)的参数由代理方法ProxyFactory（bean） 传入bean调用方法

               */
            //打算给LettuceConnectionFactory类方法增强，当其实不是而是对RedisConnection 内部方法增强
            return ProxyUtils.getProxy(bean, invocation -> {
                return new RedisAdvice(  this.applicationContext).interceptorRedisFactory(invocation);
            });
        }
        return bean;
    }

    @Override
    public int getOrder() {
        return 0;
    }
}